package com.longcheer.weibo.vo;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.longcheer.weibo.util.Utils;
import com.weibo.net.WeiboException;

public class Province {
    
    private String jsonStr;
    
    private String name;
    private int code;
    private Map<Integer, String> cityMap = new HashMap<Integer, String>();
    
    public Province(JSONObject json) throws WeiboException {
        try {
            jsonStr = json.toString();
            code = Utils.getInt("id", json);
            name = json.getString("name");
            JSONArray cities = json.getJSONArray("citys");
            for (int i = 0; i < cities.length(); ++i) {
                JSONObject city = cities.getJSONObject(0);
                @SuppressWarnings("rawtypes")
                Iterator it = city.keys();
                if (it.hasNext()) {
                    String key = it.next().toString();
                    cityMap.put(Integer.valueOf(key), city.getString(key));
                }
            }
        } catch (JSONException je) {
            throw new WeiboException(je.getMessage() + ":" + json.toString(), je);
        }
    }

    public String getName() {
        return name;
    }

    public int getCode() {
        return code;
    }

    public Map<Integer, String> getCityMap() {
        return cityMap;
    }
    
    @Override
    public String toString() {
        return jsonStr;
    }
}
